import datetime

import jqdatasdk
from django.core.management.base import BaseCommand
from stocksys import models as stocksys_models
from stocksys.management.commands.tasks_config import sys_tasks


class Command(BaseCommand):
    help = 'create_task_list'

    def handle(self, *args, **options):
        self.stdout.write(self.style.SUCCESS('清空任务数据'))
        stocksys_models.DataUpdateTask.objects.all().delete()
        try:
            user = stocksys_models.AUTH_USER.objects.get(id=1)
            try:
                sysconf = stocksys_models.SysConf.objects.get(user=user)
            except:
                raise Exception("没有初始化系统")
        except:
            raise Exception("没有创建初始用户")
        self.stdout.write(self.style.SUCCESS('创建初始任务'))

        tasks = sys_tasks()
        for task in tasks:
            stocksys_models.DataUpdateTask.objects.create(sysconf=sysconf, task_name=task['name'],
                                                          task_function=task['task_function'])

        self.stdout.write(self.style.SUCCESS('结束载入'))
